import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.fusesource.jansi.Ansi.ansi;

/**
 * @author adminhuang-lfc
 * @version 0.0.0.1-Alpha
 * @since 2023-7-5
 */

public class Log {

    public static final int INFO = 1;
    public static final int WARN = 2;
    public static final int ERROR = 3;
    public static final int FATAL = 4;
    public static final int KEY = 5;

    /**
     * 记录日志
     *
     * @param log   日志内容
     * @param level 日志等级
     * @return 是否记录成功
     */
    public static boolean log(String log, int level) {
        String log_path = GetConfig.getServerConfig().get("log_path");
        // 读日期
        LocalDate currentDate = LocalDate.now();
        // 读详细时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        // 判断文件夹是否存在
        File logDir = new File(log_path);
        if (!logDir.exists()) {
            // 创建文件夹
            logDir.mkdirs();
        }
        String fileName = log_path + currentDate + ".log";
        // 判断文件是否存在
        File file = new File(fileName);
        if (!file.exists()) {
            // 创建文件
            try{
                file.createNewFile();
            } catch (IOException e){
                e.printStackTrace();
                return false;
            }

        }
        // 写日志到文件
        try {
            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter out = new BufferedWriter(fw);
            switch (level) {
                case 1:
                    // 信息 绿色
                    out.write("\r\n" + formattedDateTime + " INFO   " + log);
                    System.out.println(ansi()
                            .eraseScreen()
                            .fgRgb(150,255,120)
                            .a(formattedDateTime + " INFO   " + log)
                            .reset());
                    break;
                case 2:
                    // 提示 蓝色
                    out.write("\r\n" + formattedDateTime + " WARN   " + log);
                    System.out.println(ansi()
                            .eraseScreen()
                            .fgRgb(100,180,255)
                            .a(formattedDateTime + " WARN   " + log)
                            .reset());
                    break;
                case 3:
                    // 警告 黄色
                    out.write("\r\n" + formattedDateTime + " ERROR  " + log);
                    System.out.println(ansi()
                            .eraseScreen()
                            .fgRgb(255,255,120)
                            .a(formattedDateTime + " ERROR  " + log)
                            .reset());
                    break;
                case 4:
                    // 错误 红色
                    out.write("\r\n" + formattedDateTime + " FATAL  " + log);
                    System.out.println(ansi()
                            .eraseScreen()
                            .fgRgb(255,50,100)
                            .a(formattedDateTime + " FATAL  " + log)
                            .reset());
                    break;
                case 5:
                    // 启动、停止运行 橙色
                    out.write("\r\n" + formattedDateTime + " KEY    " + log);
                    System.out.println(ansi()
                            .eraseScreen()
                            .fgRgb(255,150,105)
                            .a(formattedDateTime + " KEY    " + log)
                            .reset());
                    break;
                default:
                    return false;
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

//    public static void main(String[] args) throws IOException {
//        log("信息", Log.INFO);
//        log("提示", Log.WARN);
//        log("警告", Log.ERROR);
//        log("错误", Log.FATAL);
//        log("程序启动或停止", Log.KEY);
//    }
}